package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gillion.grule.client.core.GroovyShellConfig;

import com.google.common.collect.Lists;
import com.rules.admin.dao.GroovyShellModelDao;
import com.rules.admin.entity.GroovyShellModel;
import com.rules.admin.service.GroovyShellService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class GroovyShellServiceImpl extends ServiceImpl<GroovyShellModelDao, GroovyShellModel> implements GroovyShellService {
   public IPage<GroovyShellModel> queryPage(GroovyShellModel groovyShellModel, IPage<GroovyShellModel> page) {

      LambdaQueryWrapper<GroovyShellModel> queryWrapper = new LambdaQueryWrapper<>();
      if (StringUtils.isNotBlank(groovyShellModel.getProjectId())) {
         queryWrapper.eq(GroovyShellModel::getProjectId, groovyShellModel.getProjectId());
      }
      if (StringUtils.isNotBlank(groovyShellModel.getShellName())) {
         queryWrapper.eq(GroovyShellModel::getShellName,groovyShellModel.getShellName());
      }
      return this.page(page, queryWrapper);

   }

   public List<GroovyShellConfig> selectListByProjectCode(String projectCode) {
      LambdaQueryWrapper<GroovyShellModel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(GroovyShellModel::getProjectCode, projectCode);
      List<GroovyShellModel> groovyShellModelList = this.baseMapper.selectList(queryWrapper);
      List<GroovyShellConfig> groovyShellConfigList = Lists.newArrayList();
      Iterator iterator = groovyShellModelList.iterator();
      while(iterator.hasNext()) {
         GroovyShellModel model = (GroovyShellModel)iterator.next();
         GroovyShellConfig groovyShellConfig = new GroovyShellConfig();
         groovyShellConfig.setShellName(model.getShellName());
         groovyShellConfig.setScriptText(model.getScriptText());
         groovyShellConfig.setFunctionName(model.getFunctionName());
         groovyShellConfigList.add(groovyShellConfig);
      }
      return groovyShellConfigList;
   }
}
